package test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.CodeCheck;
import common.CodeChenge;

public class PostServlet extends HttpServlet {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO 自動生成されたメソッド・スタブ
		doAttrivute(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO 自動生成されたメソッド・スタブ
		doAttrivute(req, resp);
	}

	protected void doAttrivute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String title = req.getParameter("title");
		String message = req.getParameter("message");

		if (!CodeCheck.TitleCheck(title)) {
			req.setAttribute("errorMessage", "件名を正しく入力して下さい(1～100文字)");
		} else if (!CodeCheck.MessageCheck(message)) {
			req.setAttribute("errorMessage", "本文を正しく入力して下さい(1～200文字)");
		} else {

			// HTML用コード変換
			title = CodeChenge.HtmlChenge(title);
			message = CodeChenge.HtmlChenge(message);

			// 格納記事の内容
			Topic topic = new Topic();
			topic.setTitle(title);
			topic.setMessage(message);

			BlogController bc = new BlogController();
			bc.postBlogList(topic);
		}

		req.getRequestDispatcher("/read").forward(req, resp);
	}
}
